import {Component, EventEmitter, OnInit, Output} from "@angular/core";
import {
  NzNotificationService,
  UploadFile,
  NzMessageService, NzModalService
} from "ng-zorro-antd";
import {CustomReport} from "../bean/customReport.bean";
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
import {HttpServcie} from "../../../service/http.service";


@Component({
  selector: "dynamic-add",
  templateUrl: "dynamic-add.html",
  styleUrls: ["dynamic-add.scss"]
})
export class DynamicAdd implements OnInit{

  validateForm: FormGroup;
  @Output("outPut")
  outPut = new EventEmitter();
  // 定义实体
  customReport:CustomReport = new CustomReport();

  constructor(

    private fb: FormBuilder,
    private _notification: NzNotificationService,
    private httpServcie: HttpServcie,
    private modalService: NzModalService
  ) {
  }

  ngOnInit() {
    this.validateForm = this.fb.group({
      reportName: [null, [Validators.nullValidator]],
      isNullParm: [null, [Validators.nullValidator]],
      sqlSentence: [null, [Validators.nullValidator]]
    });
  }


  submitForm($event){
    // 报表名称
    if(this.customReport.reportName == undefined || this.customReport.reportName ==null){
      this._notification.create("warning", "警告提示", "报表名称不能为空！");
      return false;
    }
    // 参数是否为空
    if(this.customReport.isNullParm == undefined || this.customReport.isNullParm ==null){
      this._notification.create("warning", "警告提示", "请判断参数是否为空！");
      return false;
    }

    // SQL语句
    if (this.customReport.sqlSentence == undefined || this.customReport.sqlSentence == null) {
      this._notification.create("warning", "警告提示", "SQL语句不能为空！");
      return false;
    }
    this.customReport.createBy = JSON.parse(window.localStorage.getItem("user")).userName;



    this.httpServcie.insertReports(this.customReport).subscribe(data => {
        if (data.status == 200) {
          this._notification.create("success", "成功", "新增成功！");
          this.outPut.emit($event);
        } else {
          this._notification.create("error", "错误提示", data.message);
        }


      },
      error => {
        this._notification.create("error", "错误提示", "删除失败！");
      }
    )

  }





}
